package com.atguigu.eduservice.controller.front;

import com.atguigu.commonutils.CourseWebVoOrder;
import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.chactVo.ChapterVo;
import com.atguigu.eduservice.entity.frontVo.CourseQueryVo;
import com.atguigu.eduservice.entity.frontVo.CourseWebVo;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduCourseService;
import com.atguigu.eduservice.service.EduTeacherService;
import com.atguigu.eduservice.service.feign.OrderClient;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/eduservice/frontCourse")
@CrossOrigin
public class FrontCourseController {
    @Autowired
    EduCourseService eduCourseService;
    @Resource
    EduChapterService eduChapterService;
    @Autowired
    OrderClient orderClient;
    // 分页条件查询课程
    @PostMapping("/getCourseList/{page}/{limit}")
    public R getCourseList(@PathVariable long page,@PathVariable long limit,@RequestBody CourseQueryVo current){
        // 创建page对象
        Page<EduCourse> eduCoursePage = new Page<>(page,limit);
        Map<String,Object>resMap = eduCourseService.getCourseList(eduCoursePage,current);
        return R.ok().data(resMap);
    }
    // 根据id查询课程详细信息
    @GetMapping("/getCourseDetail/{id}")
    public R getCourseDetail(@PathVariable String id, HttpServletRequest request){
        System.out.println("getCourseDetail...."+request.getHeader("token"));
        // 查询课程详细信息
        CourseWebVo courseWebVo = eduCourseService.selectInfoWebById(id);
        // 查询当前课程的章节信息
        List<ChapterVo> chapterVos = eduChapterService.nestedList(id);
        boolean isBuy = orderClient.isBuyCourse(JwtUtils.getMemberIdByJwtToken(request), id);
        System.out.println("isBuy=="+isBuy);
        return R.ok().data("course", courseWebVo).data("chapterVoList", chapterVos).data("isBuy",isBuy);
    }
    // 根据id查询用户信息
    @GetMapping("getDto/{courseId}")
    public CourseWebVoOrder getCourseInfoDto(@PathVariable String courseId){
        CourseWebVo courseWebVo = eduCourseService.selectInfoWebById(courseId);
        CourseWebVoOrder courseWebVoOrder = new CourseWebVoOrder();
        BeanUtils.copyProperties(courseWebVo,courseWebVoOrder);
        return courseWebVoOrder;
    }
}
